package com.loan.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.loan.entity.Company;
import com.loan.entity.Result;
import com.loan.mapper.CompanyMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2021-09-28
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    CompanyMapper companyMapper;

    @RequestMapping(value = "/getCompanyList",method = RequestMethod.POST)
    public List<Company> getCompanyList(){
        List<Company> companyList = companyMapper.selectList(null);
        return companyList;
    }

    @RequestMapping(value = "/getCompanyByUid",method = RequestMethod.POST)
    public Result getCompanyByUid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);

        List<Company> companyList = companyMapper.selectByMap(columnMap);
        if (companyList.size()>0){
            Company company=companyList.get(0);
            Result result = new Result();
            result.setSuccess(true);
            result.setObj(company);
            return result;

        }

        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/deleteCompanyByUid",method = RequestMethod.POST)
    public Result deleteCompanyByUid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);

        int i = companyMapper.deleteByMap(columnMap);
        if (i>0){
            Result result = new Result();
            result.setSuccess(true);
            return result;
        }
        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/updateCompany",method = RequestMethod.POST)
    public Result updateCompany(Company company){

        Date date = new Date();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(str);
        String df = simpleDateFormat.format(date);

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("companyId",company.getCompanyId());
        updateWrapper.set("borrowInfoStatus",company.getBorrowInfoStatus());
        updateWrapper.set("lastTime",df);

        companyMapper.update(company,updateWrapper);

        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/addCompany")
    public Result addCompany(Company company){

        Date date = new Date();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(str);
        String df = simpleDateFormat.format(date);


        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Company company1=new Company();
        company1.setCompanyFullName(company.getCompanyFullName());
        company1.setCompanyAddress(company.getCompanyAddress());
        company1.setCompanyCode(company.getCompanyCode());
        company1.setLegalName(company.getLegalName());
        company1.setLegalId(company.getLegalId());
        company1.setCapital(company.getCapital());
        company1.setCommitTime(df);
        company1.setUid(uid);
        int count = companyMapper.insert(company1);

        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

}

